local M = {}

-- 获取 LSP 诊断信息
local function get_diagnostic_info()
  local diagnostics = vim.diagnostic.get(0)
  local count = { errors = 0, warnings = 0 }

  for _, diagnostic in ipairs(diagnostics) do
    if diagnostic.severity == vim.diagnostic.severity.ERROR then
      count.errors = count.errors + 1
    elseif diagnostic.severity == vim.diagnostic.severity.WARN then
      count.warnings = count.warnings + 1
    end
  end

  return count
end

-- 获取 Git 信息
local function get_git_info()
  local signs = vim.b.gitsigns_status_dict or {
    added = 0,
    changed = 0,
    removed = 0,
    head = ""
  }
  return signs
end

-- 获取 Git 分支
local function get_git_branch()
  local git = get_git_info()
  if git.head and git.head ~= "" then
    return " " .. git.head .. " "
  end
  return ""
end

-- 格式化 Git 差异信息
local function format_git_diff()
  local git = get_git_info()
  local diff_icons = {
    added = " ",
    changed = " ",
    removed = " "
  }

  local diff_info = ""
  if git.added >= 0 then
    diff_info = diff_info .. "%#StatuslineGitAdd#" .. diff_icons.added .. git.added .. "%* "
  end
  if git.changed >= 0 then
    diff_info = diff_info .. "%#StatuslineGitChange#" .. diff_icons.changed .. git.changed .. "%* "
  end
  if git.removed >= 0 then
    diff_info = diff_info .. "%#StatuslineGitDelete#" .. diff_icons.removed .. git.removed .. "%* "
  end
  return diff_info
end

-- 设置状态栏
function M.setup()
  -- 设置高亮组
  local colors = {
    error = "#db4b4b",
    warning = "#e0af68",
    info = "#0db9d7",
    hint = "#10B981",
    git_add = "#4fd6be",
    git_change = "#e0af68",
    git_delete = "#db4b4b",
    git_branch = "#7aa2f7",
    constructor = "#565f89",
    bg = "#181825",
  }

  -- 创建高亮组
  local userHighlightList = {
    {
      "StatuslineError",
      { fg = colors.error, bg=colors.bg }
    },
    {
      "StatuslineWarning",
      { fg = colors.warning, bg=colors.bg }
    },
    {
      "StatuslineGitAdd",
      { fg = colors.git_add, bg=colors.bg }
    },
    {
      "StatuslineGitChange",
      { fg = colors.git_change, bg=colors.bg }
    },
    {
      "StatuslineGitDelete",
      { fg = colors.git_delete, bg=colors.bg }
    },
    {
      "StatuslineGitBranch",
      { fg = colors.git_branch, bg=colors.bg }
    },
    {
      "StatuslineConstructor",
      { fg = colors.constructor, bg=colors.bg }
    },
  }

  for key, value in pairs(userHighlightList) do
    vim.api.nvim_set_hl(0, value[1], value[2])
  end

  -- 状态栏组件
  local error_icon = " "
  local warning_icon = " "

  -- 更新状态栏
  local function update_statusline()
    local diagnostics = get_diagnostic_info()
    local status_error = "%#StatuslineError#" .. error_icon .. diagnostics.errors .. "%* "
    local status_warning = "%#StatuslineWarning#" .. warning_icon .. diagnostics.warnings .. "%* "
    local status_git_branch = "%#StatuslineGitBranch#" .. get_git_branch() .. "%* "
    local status_git_diff = format_git_diff() .. "%="
    local status_file_name = " %t "
    local status_cursor_position = " Ln %l,Col %c"
    local file_type = " (%{&filetype}) "
    local file_spaces = " Spaces:%{shiftwidth()} "
    local file_coding = " %{''.(&fenc!=''?&fenc:&enc).''} "

    -- 组装状态栏
    local statusline_left   = "%#StatuslineConstructor#█%* " .. status_error .. status_warning .. status_file_name .. status_git_branch .. status_git_diff
    local statusline_right  = status_cursor_position .. file_spaces .. file_coding .. file_type .. "%#StatuslineConstructor#█%*"
    vim.o.statusline = statusline_left .. statusline_right
  end

  -- 创建自动命令组
  local augroup = vim.api.nvim_create_augroup("CustomStatusline", { clear = true })

  -- 注册自动命令
  vim.api.nvim_create_autocmd({
    "BufEnter",
    "BufWinEnter",
    "BufWritePost",
    "InsertEnter",
    "InsertLeave",
    "CursorHold",
    "CursorHoldI"
  }, {
    group = augroup,
    callback = update_statusline
  })

  -- 设置状态栏选项
  vim.o.laststatus = 3 -- 全局状态栏
end

return M
